package com.yiwei.zxp.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.allen.library.CircleImageView;
import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.GoodsCommentListModel;
import com.yiwei.zxp.util.DateTime;
import com.yiwei.zxp.view.StarNumberView;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class GoodsCommentAdapter extends ItemAdapter<GoodsCommentListModel.DatasBean, GoodsCommentAdapter.DefaultHolder> {



    private Context context;

    public GoodsCommentAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_goods_comment, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        GoodsCommentListModel.DatasBean item = getItem(position);

        Glide.with(context).load(item.icon).asBitmap().error(R.mipmap.img_my_default).centerCrop().into(holder.imgIcon);
        holder.txtName.setText(item.username);
        holder.txtContent.setText(item.content);
        holder.txtTime.setText(DateTime.toDate(Long.valueOf(item.time) * 1000));
        if (item.photos != null && item.photos.size() != 0) {
            holder.list.setVisibility(View.VISIBLE);
            holder.list.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
            CommentPhotoAdapter adapter = new CommentPhotoAdapter(context);
            holder.list.setAdapter(adapter);
            adapter.update(item.photos);
        } else {
            holder.list.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(item.reply_content)) {
            holder.txtSellSay.setVisibility(View.GONE);
        } else {
            holder.txtSellSay.setVisibility(View.VISIBLE);
            holder.txtSellSay.setText("商家回复："+item.reply_content);
        }
//        if (item.goods.photos != null && item.goods.photos.size() != 0) {
//            Glide.with(context).load(item.goods.photos.get(0)).error(R.color.white).into(holder.imgGoods);
//        } else {
//            Glide.with(context).load(R.color.white).into(holder.imgGoods);
//        }
//        holder.txtGoodsContent.setText(item.goods.title);
//        holder.txtGoodsMoney.setText("￥" + item.goods.price);
        holder.starView.setStar(Integer.valueOf(item.score));
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_icon)
        CircleImageView imgIcon;
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.star_view)
        StarNumberView starView;
        @BindView(R.id.txt_time)
        TextView txtTime;
        @BindView(R.id.txt_content)
        TextView txtContent;
        @BindView(R.id.list)
        RecyclerView list;
        @BindView(R.id.txt_sell_say)
        SuperButton txtSellSay;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String id);

    }
}
